﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.AI.Group;
using Verse.Noise;

namespace SZ.Mech
{
    public class CompProperties_DraginToMode2 : CompProperties
    {
        public CompProperties_DraginToMode2()
        {
            this.compClass = typeof(CompDraginToMode2);
        }
    }
    /// <summary>
    /// 变成阶段2
    /// </summary>
    public class CompDraginToMode2 : ThingComp
    {
        public Pawn pawn
        {
            get
            {
                return this.parent as Pawn;
            }
        }
        public override void CompTickRare()
        {
            base.CompTickRare();
            if (pawn.Downed)
            {
                SpawnDragonAB(pawn.Position, pawn.Map, pawn.Faction);
                pawn.DeSpawn(DestroyMode.Vanish);
            }
            else if (pawn.Dead)
            {
                SpawnDragonAB(pawn.Corpse.Position, pawn.Corpse.Map, pawn.Faction);
                pawn.Corpse.DeSpawn(DestroyMode.Vanish);
            }
        }
        public void SpawnDragonAB(IntVec3 Pos, Map map, Faction faction)
        {
            Lord lord = LordMaker.MakeNewLord(faction, new LordJob_AssaultColony(), map);

            var dragon = PawnGenerator.GeneratePawn(Kind_DefOf.SZ_DragonAB, faction);
            lord.AddPawn(dragon);
            Find.WorldPawns.PassToWorld(dragon);
            GenSpawn.Spawn(dragon, Pos, map);

            //还要召2个天兵和袭击

            List<Pawn> tianbinPawns = new List<Pawn>();
            //Pawn pGen = PawnGenerator.GeneratePawn(SZ_DefOf.SZ_HeavenForgedBattlefieldDestroyer, faction);
            //Pawn p =  (Pawn)GenSpawn.Spawn(pGen, Pos, map);
            tianbinPawns.Add(PawnGenerator.GeneratePawn(SZ_DefOf.SZ_HeavenForgedBattlefieldDestroyer, faction));
            tianbinPawns.Add(PawnGenerator.GeneratePawn(SZ_DefOf.SZ_HeavenForgedBattlefieldDestroyer, faction));
            lord.AddPawns(tianbinPawns);
            DropPodUtility.DropThingsNear(Pos, map, tianbinPawns);
            IncidentRaid(map);

        }

        public void IncidentRaid(Map map)
        {
            IncidentParms incidentParms = new IncidentParms();
            incidentParms.target = map;
            incidentParms.points = StorytellerUtility.DefaultThreatPointsNow(map);
            incidentParms.raidArrivalMode = PawnsArrivalModeDefOf.EdgeDrop;
            incidentParms.raidStrategy = RaidStrategyDefOf.ImmediateAttack;
            incidentParms.raidForceOneDowned = true;
            incidentParms.raidNeverFleeIndividual = true;//突袭从不逃跑
            incidentParms.faction = Find.FactionManager.FirstFactionOfDef(SZ_DefOf.SZ_HeavenForgedCelestiall);
            IncidentDefOf.RaidEnemy.Worker.TryExecute(incidentParms);
        }

    }

}
